﻿<UserControl x:Class="Passwords.Views.BackupView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:vm="clr-namespace:Passwords.ViewModels"
             xmlns:utils="clr-namespace:Passwords.Utils"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:models="clr-namespace:Passwords.Models"
             xmlns:controls="clr-namespace:Passwords.Controls"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
             mc:Ignorable="d" 
             d:DesignHeight="396" d:DesignWidth="745">
    <UserControl.Resources>
        <utils:CountToPasswordConverter x:Key="countConverter"/>
        <Style TargetType="Button" BasedOn="{StaticResource textButtonStyle}">
            <Setter Property="FontSize" Value="14" />
        </Style>

        <DataTemplate x:Key="ItemTemplate">
            <StackPanel x:Name="panel" Margin="5,0,5,15" Width="200" >
                <StackPanel.InputBindings>
                    <MouseBinding Gesture="LeftDoubleClick" Command="{x:Static vm:BackupCommands.Open}"/>
                </StackPanel.InputBindings>

                <TextBlock Text="{Binding Name}" FontSize="18" FontWeight="Bold"  />
                <TextBlock x:Name="meta" Text="{Binding Text}"  />
                <TextBlock x:Name="state" Text="{Binding State}"  Foreground="{DynamicResource AppAccentBrush}" />
                <StackPanel.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="{Binding rs.MiDelete, Source={StaticResource locale}}" Command="{x:Static vm:BackupCommands.Delete}"/>
                        <MenuItem Header="{Binding rs.MiExport, Source={StaticResource locale}}" Command="{x:Static vm:BackupCommands.Export}"/>
                        <MenuItem Header="{Binding rs.MiOpen, Source={StaticResource locale}}" Command="{x:Static vm:BackupCommands.Open}"/>
                        <MenuItem Header="{Binding rs.MiRestore, Source={StaticResource locale}}" Command="{x:Static vm:BackupCommands.Restore}"/>
                    </ContextMenu>
                </StackPanel.ContextMenu>
            </StackPanel>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}}" Value="True">
                    <!--<Setter Property="Foreground" Value="White" TargetName="meta"/>-->
                    <Setter Property="Foreground" Value="White" TargetName="state"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding IsUploading}" Value="true">
                    <Setter Property="TextBlock.Foreground" Value="Red" TargetName="panel"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
        
        <ControlTemplate x:Key="backupControlTemplate">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="auto"/>
                </Grid.RowDefinitions>
                <TextBlock Text="backup &amp; restore" FontSize="48" FontWeight="Light" VerticalAlignment="Top" Margin="10,5,25,20" TextTrimming="CharacterEllipsis"/>
                <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="10,0" Background="{DynamicResource TransparentChromeBrush}" >
                    <Button Content="{Binding rs.ImportBtn, Source={StaticResource locale}}" Margin="8,2,2,2" Command="{x:Static vm:BackupCommands.Import}"/>
                    <Separator Style="{StaticResource lineSepStyle}"/>
                    <Button Content="{Binding rs.ExportBtn, Source={StaticResource locale}}" Margin="2" Command="{x:Static vm:BackupCommands.Export}"/>
                    <Separator Style="{StaticResource lineSepStyle}"/>

                    <Button Content="{Binding rs.OpenBtn, Source={StaticResource locale}}" Margin="2" Command="{x:Static vm:BackupCommands.Open}"/>
                    <Separator Style="{StaticResource lineSepStyle}"/>
                    <Button Content="{Binding rs.RestoreBtn, Source={StaticResource locale}}" Margin="2" Command="{x:Static vm:BackupCommands.Restore}"/>
                    <Separator Style="{StaticResource lineSepStyle}"/>
                    <Button Content="{Binding rs.DeleteBtn, Source={StaticResource locale}}" Margin="2" Command="{x:Static vm:BackupCommands.Delete}"/>
                </StackPanel>
                <ListBox Grid.Row="2" ItemsSource="{Binding BackupModel.Backups}" ItemTemplate="{StaticResource ItemTemplate}"  
                         BorderBrush="{DynamicResource TransparentChromeBrush}"
                         Background="Transparent" TextBlock.Foreground="{DynamicResource AppForeground}"
                         SelectionMode="Extended"
                         Margin="10,0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" BorderThickness="0,0,0,1">
                    <i:Interaction.Behaviors>
                        <controls:MultiSelectBehavior SelectedItems="{Binding BackupModel.SelectedBackups, Mode=OneWayToSource}"/>
                    </i:Interaction.Behaviors>
                    <ListBox.Resources>
                        <Style TargetType="ListBoxItem" BasedOn="{StaticResource customListBoxItemStyle}"/>
                    </ListBox.Resources>
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <WrapPanel/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                </ListBox>
                <StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Center" Margin="5" VerticalAlignment="Top">
                    <StackPanel.Resources>
                        <Style TargetType="Button" BasedOn="{StaticResource textButtonStyle}">
                            <Setter Property="FontSize" Value="14" />
                        </Style>
                    </StackPanel.Resources>
                    <Button Content="{Binding rs.CloseBtn, Source={StaticResource locale}}" MinWidth="48" Command="{x:Static vm:Commands.LogoutCommand}"/>
                </StackPanel>


            </Grid>
        </ControlTemplate>
       
    </UserControl.Resources>
    <UserControl.Style>
        <Style TargetType="UserControl">
            <Style.Triggers>
                <DataTrigger Binding="{Binding AppDisplayMode}" Value="{x:Static models:AppDisplayMode.BackupAndRestore}" >
                    <Setter Property="Template" Value="{StaticResource backupControlTemplate}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Style>
</UserControl>
